import java.util.Deque;
import java.util.LinkedList;

class MyStack {
    Deque<Integer> deque;
    Deque<Integer> deque1;
    int usdSize;

    public MyStack() {
        deque = new LinkedList<>();
        deque1 = new LinkedList<>();
        usdSize = 0;
    }

    public void push(int x) {
        if (!deque.isEmpty()) {
            deque.offer(x);
            usdSize++;
        } else if (!deque1.isEmpty()) {
            deque1.offer(x);
            usdSize++;
        } else {
            deque1.offer(x);
            usdSize++;
        }
    }

    public int pop() {
        if (empty()) {
            return -1;
        }
        //size会变化
        if (!deque.isEmpty()) {
            int curSize = deque.size();
            for (int i = 0; i < curSize - 1; i++) {
                deque1.offer(deque.poll());
            }
            usdSize--;
            return deque.poll();
        } else {
            int curSize = deque1.size();
            for (int i = 0; i < curSize - 1; i++) {
                deque.offer(deque1.poll());
            }
            usdSize--;
            return deque1.poll();
        }

    }

    public int top() {
        if (empty()) {
            return -1;
        }
        //size会变化
        if (!deque.isEmpty()) {
            int curSize = deque.size();
            int ret = -1;
            for (int i = 0; i < curSize; i++) {
                ret = deque.poll();
                deque1.offer(ret);
            }
            return ret;
        } else {
            int curSize = deque1.size();
            int ret = -1;
            for (int i = 0; i < curSize; i++) {
                ret = deque1.poll();
                deque.offer(ret);
            }
            return ret;

        }
    }

        //空
        public boolean empty () {
            return usdSize == 0;
        }



}